<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/3/3
  Time: 14:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        //创建XHr
        function XHR() {
            var xhr= null;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xhr=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xhr=new ActiveXObject("Microsoft.XMLHTTP");
            }
            return xhr;
        }
        // ajax的核心操作
        function getText() {
          // 1.创建xmlhttprequest 对象
             var xhr= XHR();
            // 2. 与服务器建立连接并发送请求
            xhr.open("get","getText",true);
            xhr.send(null);
            // 3. 服务器端获取到数据 然后再前台页面接收
            xhr.onreadystatechange=function()
            {
                if (xhr.readyState==4 && xhr.status==200)
                {
                     var txt=xhr.responseText;
                     // 需要将从后台返回的json 串转换为对象 最后才能想换换为对象中的数据
                     var objects= eval("("+txt+")");
                     alert(objects.id);
                }
            }


        }

    </script>
</head>
<body>
 <a href="###" onclick="getText()">AJAX请求</a>
</body>
</html>
